package org.edu.dao.impl;


import java.util.List;

import javax.annotation.Resource;

import org.edu.bean.Role;
import org.edu.bean.Role_user;
import org.edu.dao.RoleDao;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;



@Service
public class RoleDaoImpl implements RoleDao {

	
	@Resource
	private HibernateTemplate hibernateTemplate;
	
	@SuppressWarnings("unchecked")
	public Role selectRoleFromUser(Role_user roleUser) {
		List ls = hibernateTemplate.find("from Role r where r.id = ?",roleUser.getRoleId());
		if (ls.size()>0) {
			return (Role)ls.get(0);
		}
		return null ;
	}
	
	@SuppressWarnings("unchecked")
	public List<Role_user> selectRole_User(int userId){
		return hibernateTemplate.find("from Role_user ru where ru.userId = ?",userId) ;
	}

	@SuppressWarnings("unchecked")
	public List<Role> selectRoles() {
		return hibernateTemplate.find("from Role");
	}

	public Integer createRole(Role role) {
		return (Integer)hibernateTemplate.save(role);
	}

	public boolean delRole(Role role) {
		hibernateTemplate.delete(role);
		return true;
	}

	public Role getRoleById(int id) {
		return (Role) hibernateTemplate.find("from Role r where r.id ="+id).get(0);
	}
	
	
	
	
}
